#!/bin/bash
# Copyright (c) 2025 HiSilicon (Shanghai) Technologies Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
 
set -e
OHOS_ROOT_PATH=$1
OHOS_OUTDIR=$2
OHOS_LITE=$3
COMPILER_DIR=$4
CHIP=$6
 
export KERNEL_VERSION="$5"
 
if [ -z "${OHOS_ROOT_PATH}" ];then
    OHOS_ROOT_PATH=$(pwd)/../../../..
else
    echo "OHOS_ROOT_PATH=${OHOS_ROOT_PATH}"
fi
 
export OHOS_ROOT_PATH
export OHOS_OUTDIR
if [ ${COMPILER_DIR} != "" ];then
    export COMPILER_PATH=${COMPILER_DIR}/bin
fi
 
SDK_LINUX_SRC_PATH=${OHOS_ROOT_PATH}/device/soc/hisilicon/hi3403v100/sdk_linux
SDK_LINUX_TMP_PATH=${OHOS_OUTDIR}/sdk_linux/src_tmp
SDK_LINUX_SMP_PATH=${SDK_LINUX_TMP_PATH}/smp
SDK_LINUX_OPEN_PATH=${SDK_LINUX_TMP_PATH}/open_source
SDK_LINUX_ATF_PATH=${SDK_LINUX_TMP_PATH}/open_source/trusted-firmware-a
SYSROOT_PATH=${OHOS_OUTDIR}/sysroot
export SYSROOT_PATH
OSDRV_CROSS_PATH=${OHOS_ROOT_PATH}/prebuilts/gcc/linux-x86/aarch64/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu
 
rm -rdf ${SDK_LINUX_TMP_PATH}; mkdir -p ${SDK_LINUX_TMP_PATH}
mkdir -p ${SDK_LINUX_SMP_PATH}
cp -rf ${SDK_LINUX_SRC_PATH}/smp/* ${SDK_LINUX_SMP_PATH}
cp -rf ${SDK_LINUX_SRC_PATH}/*.patch ${SDK_LINUX_SMP_PATH}
cp -rf ${SDK_LINUX_SRC_PATH}/001_hi3403v100_mpp.patch ${SDK_LINUX_TMP_PATH}
 
mkdir -p ${SDK_LINUX_OPEN_PATH}
mkdir -p ${SDK_LINUX_ATF_PATH}
cp -rf ${SDK_LINUX_SRC_PATH}/open_source/trusted-firmware-a/* ${SDK_LINUX_ATF_PATH}
cp -rf ${SDK_LINUX_SRC_PATH}/002_hi3403v100_trusted_firmware.patch ${SDK_LINUX_ATF_PATH}
cp -rf ${SDK_LINUX_SRC_PATH}/open_source/mbedtls ${SDK_LINUX_OPEN_PATH}/
 
echo "Add patchs to sdk..."
pushd ${SDK_LINUX_SMP_PATH}
patch -p1 < ./001_hi3403v100_mpp.patch
popd
 
echo "Add patchs to atf..."
pushd ${SDK_LINUX_ATF_PATH}
patch -p1 < ./002_hi3403v100_trusted_firmware.patch
popd

echo "compile ko..."
 pushd "${SDK_LINUX_SMP_PATH}/a55_linux/mpp/out/obj" && \
    make clean OHOS_LITE=y CHIP="${CHIP}" SYSROOT_PATH="${SYSROOT_PATH}" && \
    make -j OHOS_LITE=y CHIP="${CHIP}" SYSROOT_PATH="${SYSROOT_PATH}" && popd
echo "compile atf..."
 pushd ${SDK_LINUX_OPEN_PATH}/trusted-firmware-a && make clean OHOS_LITE=y && 
 make -j OHOS_LITE=y CHIP=${CHIP} KERNEL_VER=${KERNEL_VERSION} OSDRV_CROSS=${OSDRV_CROSS_PATH}&& popd
 
sdk_libs_name_set=(
  "libaac_comm.so",
  "libaac_dec.so",
  "libaac_enc.so",
  "libaac_sbr_dec.so",
  "libaac_sbr_enc.so",
  "libaiv.so",
  "libot_acodec.so",
  "libot_adec.so",
  "libot_aenc.so",
  "libot_ai.so",
  "libot_aio.so",
  "libot_ao.so",
  "libot_base.so",
  "libot_chnl.so",
  "libot_dis.so",
  "libot_gdc.so",
  "libot_gyrodis.so",
  "libot_h264e.so",
  "libot_h265e.so",
  "libot_irq.so",
  "libot_isp.so",
  "libot_jpegd.so",
  "libot_jpege.so",
  "libot_motionfusion_user.so",
  "libot_mpi_isp.so",
  "libot_osal.so",
  "libot_rc.so",
  "libot_rgn.so",
  "libot_sensor_i2c.so",
  "libot_sensor_spi.so",
  "libot_sys.so",
  "libot_vdec.so",
  "libot_vedu.so",
  "libot_venc.so",
  "libot_vfmw.so",
  "libot_vgs.so",
  "libot_vi.so",
  "libot_vo.so",
  "libot_vpp.so",  
  "libot_vpss.so",
  "libsecurec.so",
  "libsns_hy_s0603.so",
  "libss_acs.so",
  "libss_ae.so",
  "libss_awb.so",
  "libss_bnr.so",
  "libss_calcflicker.so",
  "libss_cipher.so",
  "libss_crb.so",
  "libss_dehaze.so",
  "libss_dnvqe.so",
  "libss_dpu_match.so",
  "libss_dpu_rect.so",
  "libss_drc.so",
  "libss_extend_stats.so",
  "libss_fisheye_calibrate.so",
  "libss_hdmi.so",
  "libss_hnr.so",
  "libss_ir_auto.so",
  "libss_isp.so",
  "libss_ive.so",
  "libss_klad.so",
  "libss_ldci.so",
  "libss_mau.so",
  "libss_md.so",
  "libss_motionfusion.so",
  "libss_mpi.so",
  "libss_otp.so",
  "libss_pciv.so",
  "libss_pqp.so",
  "libss_snap.so",
  "libss_tde.so",
  "libss_upvqe.so",
  "libss_voice_engine.so",
  "libvqe_aec.so",
  "libvqe_agc.so",
  "libvqe_anr.so",
  "libvqe_common.so",
  "libvqe_eq.so",
  "libvqe_hpf.so",
  "libvqe_record.so",
  "libvqe_res.so",
  "libvqe_talkv2.so",
  "svp_npu/libprotobuf-c.so.1",
  "svp_npu/libsvp_acl.so",
  "svp_npu/libsvp_aicpu.so",
)
 
pushd ${SDK_LINUX_SMP_PATH}/a55_linux/mpp/out/lib
for lib_name in "${sdk_libs_name_set[@]}"; do
    if [ -f "$lib_name" ]; then
        cp -f $lib_name ${OHOS_OUTDIR}
    fi
done
popd
 
mkdir -p ${SDK_LINUX_TMP_PATH}/out
cp -rf ${SDK_LINUX_SMP_PATH}/a55_linux/mpp/out/ko ${SDK_LINUX_TMP_PATH}/out
 
# copy images and burn_table file
cp -rf ${SDK_LINUX_SRC_PATH}/../burn/* ${OHOS_OUTDIR}
# cp uImage，exe atf, flip.bin改名uImage，并替换掉；
cp -rf ${SDK_LINUX_OPEN_PATH}/trusted-firmware-a/arm-trusted-firmware-2.2/build/${CHIP}/release/fip.bin ${OHOS_OUTDIR}
# $(hide) cp -rf $(KERNEL_OBJ_TMP_PATH)/arch/$(KERNEL_ARCH)/boot/uImage $(OUT_DIR)